Private Sub txtUpper_KeyPress(KeyAscii As Integer) KeyAscii = Asc(UCase$(Chr$(KeyAscii))) End Sub
The only problem with the KeyPress event is that every keystroke is trapped by this routine. This is a lot of extra work for the program, and it can slow down the input process somewhat. An alternate method is to convert everything to uppercase once the user tabs out of the text box. For that, you can use code like the following:
Sub txtUpper_LostFocus()
txtUpper.Text = UCase$(txtUpper.Text)
End Sub
Although the user must wait until he or she tabs out of the field to see the results converted to uppercase, the end result is the same. The LostFocus() can also be fired when the user clicks to another control with the mouse.
Lowercase Input
As is the case with the uppercase input example, you only need one line of code placed into the KeyPress event of a text box to covert every key pressed to lowercase. In this example, you use the LCase$() function to perform the keystroke conversion:
Private Sub txtLower_KeyPress(KeyAscii As Integer) KeyAscii = Asc(LCase$(Chr$(KeyAscii))) End Sub
Input Numbers Only
Sometimes you might want to have the user only input numeric values into a text box. You use the KeyPress event for this type of validation as well. Heres an example:
Private Sub txtNumbers_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyDelete ' Delete Key
Case vbKeyBack ' Backspace Key
Case 48 To 57 ' Number 0-9
Case Else